﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ket_noi_toi_VPP_bang_mo_hinh_ba_lop.DAL;

namespace Ket_noi_toi_VPP_bang_mo_hinh_ba_lop
{
    public partial class FormNhaCungCap : Form
    {
        NhaCungCap NCC = new NhaCungCap();
        public FormNhaCungCap()
        {
            InitializeComponent();
        }

        private void FormNhaCungCap_Load(object sender, EventArgs e)
        {
            dtgHienThi.DataSource = NCC.SelectAll();
        }

        private void btReset_Click(object sender, EventArgs e)
        {
            this.tbMaNCC.Clear();
            this.tbTenNCC.Clear();
            this.tbDiaChi.Clear();
            this.tbDT.Clear();
            this.tbEmail.Clear();
            this.tbMaNCC.Focus();
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            NCC.Insert( tbTenNCC.Text, tbDiaChi.Text, tbDT.Text, tbEmail.Text);
            dtgHienThi.DataSource = NCC.SelectAll();
            FormNhaCungCap_Load(sender,e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            NCC.Delete(Convert.ToInt32(tbMaNCC.Text));
            dtgHienThi.DataSource = NCC.SelectAll();
        }

        private void btSelect_Click(object sender, EventArgs e)
        {
            dtgHienThi.DataSource = NCC.SelectID(Convert.ToInt32(tbMaNCC.Text));
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            NCC.Update(Convert.ToInt32(tbMaNCC.Text), tbTenNCC.Text, tbDiaChi.Text, tbDT.Text, tbEmail.Text);
            dtgHienThi.DataSource = NCC.SelectAll();
            FormNhaCungCap_Load(sender, e);
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtgHienThi_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string ID;
            int dong;
            dong = e.RowIndex;
            ID = dtgHienThi.Rows[dong].Cells[3].Value.ToString();
            this.tbMaNCC.Text = dtgHienThi.Rows[dong].Cells[0].Value.ToString();
            this.tbTenNCC.Text = dtgHienThi.Rows[dong].Cells[1].Value.ToString();
            this.tbDiaChi.Text = dtgHienThi.Rows[dong].Cells[2].Value.ToString();
            this.tbDT.Text = dtgHienThi.Rows[dong].Cells[3].Value.ToString();
            this.tbEmail.Text = dtgHienThi.Rows[dong].Cells[4].Value.ToString();
        }

        private void btGoBack_Click(object sender, EventArgs e)
        {
            FormNhaCungCap_Load(sender,e);
        }
    }
}
